package tvdi

import java.util.List;

class AlunoController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [alunoInstanceList: Aluno.list(params), alunoInstanceTotal: Aluno.count()]
    }

	def download = {
		Aluno aluno = Aluno.get(params.id);
		Modulo modulo = Modulo.get(aluno.modulo.id);
		Curso curso = Curso.get(aluno.modulo.curso.id);
		//imagens
		params.layout = grailsApplication.parentContext.getResource("/images/layout.png").file.getAbsolutePath().replace("\\","/");
		params.LOGO = grailsApplication.parentContext.getResource("/images/logo_p.png").file.getAbsolutePath().replace("\\","/");
		params.LOGO_CNPQ = grailsApplication.parentContext.getResource("/images/Logo_cnpq.jpg").file.getAbsolutePath().replace("\\","/");
		params.LOGO_CCN = grailsApplication.parentContext.getResource("/images/logo_ccn.png").file.getAbsolutePath().replace("\\","/");
		params.LOGO_UERN = grailsApplication.parentContext.getResource("/images/LogoUERN.png").file.getAbsolutePath().replace("\\","/");
		
		params._format = "PDF";
		params._name = "Certificado_${aluno.id}";
		params._file = "certificado.jasper";
		
		//List listAlunos = new ArrayList();
		//List listModulos = new ArrayList();
		//List listCursos = new ArrayList();
		//listAlunos.add(aluno);
		//listModulos.add(modulo);
		//listCursos.add(curso);
		
		Certificado c = new Certificado();
		c.nomeAluno = aluno.nome;
		c.nomeModulo= modulo.nome;
		c.nomeCurso=curso.nome;
		c.dataInicio= modulo.dataInicio;
		c.dataFim= modulo.dataFim;
		c.cargaHoraria=modulo.cargaHoraria;
		def certificados = [c];
		
		chain(controller:'jasper',action:'index',model:[data: certificados], params:params)
	}
	
	class Certificado {
		
		String nomeAluno
		String nomeModulo
		String nomeCurso
		Date dataInicio
		Date dataFim
		Integer cargaHoraria
	}
	
    def create = {
        def alunoInstance = new Aluno()
        alunoInstance.properties = params
        return [alunoInstance: alunoInstance]
    }

    def save = {
        def alunoInstance = new Aluno(params)
        if (alunoInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'aluno.label', default: 'Aluno'), alunoInstance.id])}"
            redirect(action: "show", id: alunoInstance.id)
        }
        else {
            render(view: "create", model: [alunoInstance: alunoInstance])
        }
    }

    def show = {
        def alunoInstance = Aluno.get(params.id)
        if (!alunoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'aluno.label', default: 'Aluno'), params.id])}"
            redirect(action: "list")
        }
        else {
            [alunoInstance: alunoInstance]
        }
    }

    def edit = {
        def alunoInstance = Aluno.get(params.id)
        if (!alunoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'aluno.label', default: 'Aluno'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [alunoInstance: alunoInstance]
        }
    }

    def update = {
        def alunoInstance = Aluno.get(params.id)
        if (alunoInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (alunoInstance.version > version) {
                    
                    alunoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'aluno.label', default: 'Aluno')] as Object[], "Another user has updated this Aluno while you were editing")
                    render(view: "edit", model: [alunoInstance: alunoInstance])
                    return
                }
            }
            alunoInstance.properties = params
            if (!alunoInstance.hasErrors() && alunoInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'aluno.label', default: 'Aluno'), alunoInstance.id])}"
                redirect(action: "show", id: alunoInstance.id)
            }
            else {
                render(view: "edit", model: [alunoInstance: alunoInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'aluno.label', default: 'Aluno'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def alunoInstance = Aluno.get(params.id)
        if (alunoInstance) {
            try {
                alunoInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'aluno.label', default: 'Aluno'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'aluno.label', default: 'Aluno'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'aluno.label', default: 'Aluno'), params.id])}"
            redirect(action: "list")
        }
    }
}
